package com.collection.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.ResultSet;

public class DBUtil {
	
	private String dbUrl;
	
	private String driverClass;
	
	private String userName;
	
	private String password;
	
	private Connection conn;
	
	public DBUtil(){
		
	}
	
	public DBUtil(String dbUrl, String driverClass, String userName, String password){
		this.dbUrl = dbUrl;
		this.driverClass = driverClass;
		this.userName = userName;
		this.password = password;
	}
	
	public Connection openConnction() {
		try {
			if(driverClass != null && dbUrl != null) {
				Class.forName(driverClass);
				conn = DriverManager.getConnection(dbUrl, userName, password);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public void close(Connection conn, Statement stm, ResultSet rs) {
		try {
			if(rs != null) {
				rs.close();
				rs = null;
			}
			
			if(stm != null) {
				stm.close();
				stm = null;
			}
			
			if(conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getDbUrl() {
		return dbUrl;
	}

	public void setDbUrl(String dbUrl) {
		this.dbUrl = dbUrl;
	}

	public String getDriverClass() {
		return driverClass;
	}

	public void setDriverClass(String driverClass) {
		this.driverClass = driverClass;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Connection getConn() {
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}
}
